package com.example.app;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.os.AsyncTaskCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.BaseActivity;

public class MainAppActivity extends BaseActivity {
	private Context mContext;
	private ActivityInfo[] actInfors;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_app);
		mContext = this;
		setTitle(R.string.app_name);
		
		ListView listView = (ListView) findViewById(android.R.id.list);
		final ActivityAdapter adapter = new ActivityAdapter();
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				ActivityInfo info = (ActivityInfo) arg0.getAdapter().getItem(
						arg2);
				if (info != null && !"com.example.app.MainAppActivity".equals(info.name)) {
					Intent it = new Intent();
					it.setClassName(mContext.getPackageName(), info.name);
					startActivity(it);
				}
			}
		});

		AsyncTaskCompat
				.executeParallel(new AsyncTask<Void, Void, ArrayList<ActivityInfo>>() {
					@Override
					protected ArrayList<ActivityInfo> doInBackground(
							Void... params) {
						PackageInfo infor = null;
						try {
							infor = mContext.getPackageManager()
									.getPackageInfo(mContext.getPackageName(),
											PackageManager.GET_ACTIVITIES);
							actInfors = infor.activities;
						} catch (NameNotFoundException e) {
							e.printStackTrace();
						}
						return null;
					}

					/*
					 * (non-Javadoc)
					 * 
					 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
					 */
					@Override
					protected void onPostExecute(ArrayList<ActivityInfo> result) {
						adapter.notifyDataSetChanged();
					}
				});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main_app, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	class ActivityAdapter extends BaseAdapter {
		/*
		 * (non-Javadoc)
		 * 
		 * @see android.widget.Adapter#getCount()
		 */
		@Override
		public int getCount() {
			return actInfors != null ? actInfors.length : 0;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.widget.Adapter#getItem(int)
		 */
		@Override
		public Object getItem(int position) {
			return actInfors == null || position >= getCount() ? null
					: actInfors[position];
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.widget.Adapter#getItemId(int)
		 */
		@Override
		public long getItemId(int position) {
			return position;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.widget.Adapter#getView(int, android.view.View,
		 * android.view.ViewGroup)
		 */
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView tview = null;
			if (convertView == null) {
				tview = (TextView) View.inflate(mContext,
						android.R.layout.simple_list_item_1, null);
			} else {
				tview = (TextView) convertView;
			}
			ActivityInfo info = (ActivityInfo) getItem(position);
			//
			if (info.labelRes <= 0) {
				CharSequence ch=info.loadLabel(mContext.getPackageManager());
				tview.setText(ch);
			} else {
				tview.setText(info.labelRes);
			}	
			return tview;
		}

	}
}
